import _ from 'lodash';
import * as api from '@/services/ContractPlanMgr';
import * as utils from '../utils/treeUtils';

const Model = {
  namespace: 'ContractPlanEntireProjectItems',
  state: {
    all: [],
    searched: [],
    overAllYears: [],
    expandedRowKeys: [],
    searchItems: {},
    currentYear: '',
    currentShowGroup: '期初存货/新供/综合',
    currentMonths: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '14'],
  },
  effects: {
    *init({ payload, callback }, { call, put, select }) {
      yield put({
        type: 'save',
        payload: !payload.params.code
          ? {
              all: [],
              searched: [],
              overAllYears: [],
              expandedRowKeys: [],
              searchItems: {},
            }
          : {
              all: [],
              searched: [],
              overAllYears: [],
              expandedRowKeys: [],
            },
      });
      let currentYear = yield select(state => state.ContractPlanEntireProjectItems.currentYear);
      currentYear = currentYear ? currentYear : '';
      const params = { ...payload.params, year: currentYear };
      const response = yield call(api.getOverallSignPlanVersionUsingGET, { params });
      if (response && response.data && !_.isEmpty(response.data)) {
        const all = response.data.detailList;
        const overAllYears = response.data.timeList;

        if (currentYear === '') {
          if (_.includes(overAllYears, new Date().getFullYear().toString())) {
            currentYear = new Date().getFullYear().toString();
          } else {
            currentYear = _.first(overAllYears);
          }
        }

        const searchItems = utils.getSearchItems(all);
        // 获取展开的列key
        const expandedRowKeys = utils.getExpandedRowKeys(all, 'signDetailId');
        const searched = utils.buildTree(all);
        yield put({
          type: 'save',
          payload: !payload.params.code
            ? { all, searched, overAllYears, expandedRowKeys, searchItems, currentYear }
            : { all, searched, overAllYears, expandedRowKeys, currentYear },
        });
      }
      if (callback) callback(response && response.data ? response.data : []);
    },

    *changeCurrentYear({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { currentYear: payload.currentYear },
      });
      if (callback) callback();
    },
  },
  reducers: {
    changeCurrentMonths(state, action) {
      let currentMonths = action.payload.currentMonths;
      if (currentMonths == null || currentMonths.length === 0) {
        currentMonths = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '14'];
      }
      return {
        ...state,
        currentMonths: currentMonths,
      };
    },
    changeCurrentShowGroup(state, action) {
      let currentShowGroup = action.payload.currentShowGroup;
      return {
        ...state,
        currentShowGroup,
      };
    },

    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    clear() {
      return {
        all: [],
        searched: [],
        overAllYears: [],
        expandedRowKeys: [],
        searchItems: {},
        currentMonths: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '14'],
      };
    },
  },
};
export default Model;
